﻿using IceZero.DataStore.Models;
using IceZero.DataStore.Models.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IceZero.DataStore.InitDB
{
    public partial class DbInitializer
    {
        /// <summary>
        /// 填充0到32位长度
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        private string FillZeroTo32(int num)
        {
            var numStr = num.ToString();
            var maxLength = 32;
            if (numStr.Length >= maxLength)
            {
                return numStr;
            }
            var needCount = maxLength - numStr.Length;
            var prefix = string.Join("", Enumerable.Repeat('0', needCount));
            return prefix + numStr;
        }
        public void InitOperationAction()
        {
            var dataStore = new OperationActionDataStore();
            var userAdmin = new UserDataStore().GetAdmin();
            if (dataStore.Count() > 0)
            {
                return;
            }
            var list = new List<OperationAction>() {
                new OperationAction()
                {
                ActionCode = "view",
                ActionName = "浏览",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            },new OperationAction()
            {
                ActionCode = "click",
                ActionName = "点击",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            },new OperationAction()
            {
                ActionCode = "sign_in",
                ActionName = "签到",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            },new OperationAction()
            {
                ActionCode = "cancel_sign_in",
                ActionName = "取消签到",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            },new OperationAction()
            {
                ActionCode = "clock_in",
                ActionName = "打卡",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            },new OperationAction()
            {
                ActionCode = "cancel_clock_in",
                ActionName = "取消打卡",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            },new OperationAction()
            {
                ActionCode = "like_it",
                ActionName = "点赞",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            },new OperationAction()
            {
                ActionCode = "cancel_like_it",
                ActionName = "取消点赞",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            },new OperationAction()
            {
                ActionCode = "collect",
                ActionName = "收藏",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            },new OperationAction()
            {
                ActionCode = "cancel_collect",
                ActionName = "取消收藏",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            },new OperationAction()
            {
                ActionCode = "comment",
                ActionName = "评论",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            },new OperationAction()
            {
                ActionCode = "cancel_comment",
                ActionName = "取消评论",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            },new OperationAction()
            {
                ActionCode = "add",
                ActionName = "添加",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            }
            ,new OperationAction()
            {
                ActionCode = "edit",
                ActionName = "编辑",
                Remark = "系统内置行为,即修改",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            }
            ,new OperationAction()
            {
                ActionCode = "delete",
                ActionName = "删除",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            }
            ,new OperationAction()
            {
                ActionCode = "restore",
                ActionName = "恢复",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            }
            ,new OperationAction()
            {
                ActionCode = "backup",
                ActionName = "备份",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            }
            ,new OperationAction()
            {
                ActionCode = "like",
                ActionName = "喜欢",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            }
            ,new OperationAction()
            {
                ActionCode = "cancel_like",
                ActionName = "不喜欢",
                Remark = "系统内置行为,同unlike",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            }

            ,new OperationAction()
            {
                ActionCode = "vote",
                ActionName = "投票",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            }
            ,new OperationAction()
            {
                ActionCode = "cancel_vote",
                ActionName = "投票",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            }
            ,new OperationAction()
            {
                ActionCode = "learn",
                ActionName = "学习",
                Remark = "系统内置行为",
                CreatorId = userAdmin.Id,
                UpdaterId = userAdmin.Id,
            }
            };
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Id = FillZeroTo32(i + 1);
            }
            dataStore.BatchSave(list);
        }

        /// <summary>
        /// 
        /// </summary>
        public void InitOperationActionCombo()
        {
            var dataStore = new OperationActionComboDataStore();
            var oaDataStore = new OperationActionDataStore();
            var userAdmin = new UserDataStore().GetAdmin();
            if (dataStore.Count() > 0)
            {
                return;
            }
            var list = new List<OperationActionCombo>()
            {
                new OperationActionCombo()
                {
                    ActionComboCode = "combo_view",
                    Remark = "浏览",
                    IsLoop = true,
                    IsMutexAction = false,
                    PositiveActionId = oaDataStore.QueryOne("ActionCode", "view").Id,
                    CreatorId = userAdmin.Id,
                    UpdaterId = userAdmin.Id,
                },
                new OperationActionCombo()
                {
                    ActionComboCode = "combo_click",
                    Remark = "点击",
                    IsLoop = true,
                    IsMutexAction = false,
                    PositiveActionId = oaDataStore.QueryOne("ActionCode", "click").Id,
                    CreatorId = userAdmin.Id,
                    UpdaterId = userAdmin.Id,
                },
                new OperationActionCombo()
                {
                    ActionComboCode = "combo_sign_in",
                    Remark = "签到",
                    IsLoop = false,
                    IsMutexAction = false,
                    PositiveActionId = oaDataStore.QueryOne("ActionCode", "sign_in").Id,
                    CreatorId = userAdmin.Id,
                    UpdaterId = userAdmin.Id,
                },
                new OperationActionCombo()
                {
                    ActionComboCode = "combo_clock_in",
                    Remark = "打卡",
                    IsLoop = false,
                    IsMutexAction = false,
                    PositiveActionId = oaDataStore.QueryOne("ActionCode", "sign_in").Id,
                    CreatorId = userAdmin.Id,
                    UpdaterId = userAdmin.Id,
                }
                ,new OperationActionCombo()
                {
                    ActionComboCode = "combo_like_it",
                    Remark = "点赞",
                    IsLoop = true,
                    IsMutexAction = true,
                    PositiveActionId = oaDataStore.QueryOne("ActionCode", "like_it").Id,
                    OppositeActionId = oaDataStore.QueryOne("ActionCode", "cancel_like_it").Id,
                    CreatorId = userAdmin.Id,
                    UpdaterId = userAdmin.Id,
                }
                ,new OperationActionCombo()
                {
                    ActionComboCode = "combo_like_it_2",
                    Remark = "点赞,只有点赞，没有取消",
                    IsLoop = true,
                    IsMutexAction = false,
                    PositiveActionId = oaDataStore.QueryOne("ActionCode", "like_it").Id,
                    CreatorId = userAdmin.Id,
                    UpdaterId = userAdmin.Id,
                }
                , new OperationActionCombo()
                {
                    ActionComboCode = "combo_collect",
                    Remark = "收藏",
                    IsLoop = true,
                    IsMutexAction = true,
                    PositiveActionId = oaDataStore.QueryOne("ActionCode", "collect").Id,
                    OppositeActionId = oaDataStore.QueryOne("ActionCode", "cancel_collect").Id,
                    CreatorId = userAdmin.Id,
                    UpdaterId = userAdmin.Id,
                }
                , new OperationActionCombo()
                {
                    ActionComboCode = "combo_like",
                    Remark = "是否喜欢",
                    IsLoop = false,
                    IsMutexAction = true,
                    PositiveActionId = oaDataStore.QueryOne("ActionCode", "like").Id,
                    OppositeActionId = oaDataStore.QueryOne("ActionCode", "cancel_like").Id,
                    CreatorId = userAdmin.Id,
                    UpdaterId = userAdmin.Id,
                }
                , new OperationActionCombo()
                {
                    ActionComboCode = "combo_add",
                    Remark = "添加",
                    IsLoop = true,
                    IsMutexAction = false,
                    PositiveActionId = oaDataStore.QueryOne("ActionCode", "add").Id,
                    CreatorId = userAdmin.Id,
                    UpdaterId = userAdmin.Id,
                }
                , new OperationActionCombo()
                {
                    ActionComboCode = "combo_edit",
                    Remark = "编辑",
                    IsLoop = true,
                    IsMutexAction = false,
                    PositiveActionId = oaDataStore.QueryOne("ActionCode", "edit").Id,
                    CreatorId = userAdmin.Id,
                    UpdaterId = userAdmin.Id,
                }
                , new OperationActionCombo()
                {
                    ActionComboCode = "combo_delete",
                    Remark = "删除",
                    IsLoop = true,
                    IsMutexAction = false,
                    PositiveActionId = oaDataStore.QueryOne("ActionCode", "delete").Id,
                    CreatorId = userAdmin.Id,
                    UpdaterId = userAdmin.Id,
                }
                , new OperationActionCombo()
                {
                    ActionComboCode = "combo_restore",
                    Remark = "恢复",
                    IsLoop = true,
                    IsMutexAction = false,
                    PositiveActionId = oaDataStore.QueryOne("ActionCode", "restore").Id,
                    CreatorId = userAdmin.Id,
                    UpdaterId = userAdmin.Id,
                }
                  , new OperationActionCombo()
                {
                    ActionComboCode = "combo_backup",
                    Remark = "备份",
                    IsLoop = true,
                    IsMutexAction = false,
                    PositiveActionId = oaDataStore.QueryOne("ActionCode", "backup").Id,
                    CreatorId = userAdmin.Id,
                    UpdaterId = userAdmin.Id,
                }
                   , new OperationActionCombo()
                {
                    ActionComboCode = "combo_vote",
                    Remark = "投票",
                    IsLoop = false,
                    IsMutexAction = false,
                    PositiveActionId = oaDataStore.QueryOne("ActionCode", "vote").Id,
                    CreatorId = userAdmin.Id,
                    UpdaterId = userAdmin.Id,
                }
                   , new OperationActionCombo()
                {
                    ActionComboCode = "combo_learn",
                    Remark = "学习",
                    IsLoop = true,
                    IsMutexAction = false,
                    PositiveActionId = oaDataStore.QueryOne("ActionCode", "learn").Id,
                    CreatorId = userAdmin.Id,
                    UpdaterId = userAdmin.Id,
                }
            };
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Id = FillZeroTo32(i + 1);
            }
            dataStore.BatchSave(list);
        }

        /// <summary>
        /// 
        /// </summary>
        public void InitStatisticModels()
        {
            InitOperationAction();
            InitOperationActionCombo();
        }
    }
}
